package com.videoweb;

import dev.langchain4j.chain.ConversationalChain;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.service.AiServices;

import java.time.LocalDate;
import java.util.concurrent.CompletableFuture;

public class Chat {


    public static void main(String[] args) {
        ChatModel chatModel = OpenAiChatModel.builder()
                .apiKey(ApiKeys.API_KEY)
                .baseUrl(ApiKeys.API_BASE_URL)
                .modelName(ApiKeys.MODEL_NAME)

                .build();

        ChatMemory memory = MessageWindowChatMemory.withMaxMessages(20);
//        添加对话链
        ConversationalChain chain = ConversationalChain.builder()
                .chatModel(chatModel)
                .chatMemory(memory)
                .build();





        System.out.println("1");

        String answer = chain.execute("说说杨千嬅");
        System.out.println(answer);

        System.out.println("2");

        answer = chain.execute("他多少岁了？"+ LocalDate.now());
        System.out.println(answer);


//    ConversationalChain chain = OpenAiChatModel.builder()
//            .build();

//    AiServices services =

//        StreamingChatModel streamingChatModel = OpenAiStreamingChatModel.builder()
//                .apiKey(ApiKeys.API_KEY)
//                .modelName(ApiKeys.MODEL_NAME)
//                .baseUrl(ApiKeys.API_BASE_URL) // 关键：重定向到硅基流动API
//                .build();
//
//        CompletableFuture<ChatResponse> futureChatResponse = new CompletableFuture<>();
//
//        streamingChatModel.chat("说说杨千嬅", new StreamingChatResponseHandler() {
//
//            @Override
//            public void onPartialResponse(String partialResponse) {
//                System.out.print(partialResponse);
//            }
//
//            @Override
//            public void onCompleteResponse(ChatResponse completeResponse) {
//                futureChatResponse.complete(completeResponse);
//            }
//
//            @Override
//            public void onError(Throwable error) {
//                futureChatResponse.completeExceptionally(error);
//            }
//        });
//
//        futureChatResponse.join();
//
//
//        streamingChatModel.chat("她多少岁了", new StreamingChatResponseHandler() {
//
//            @Override
//            public void onPartialResponse(String partialResponse) {
//                System.out.print(partialResponse);
//            }
//
//            @Override
//            public void onCompleteResponse(ChatResponse completeResponse) {
//                futureChatResponse.complete(completeResponse);
//            }
//
//            @Override
//            public void onError(Throwable error) {
//                futureChatResponse.completeExceptionally(error);
//            }
//        });
//
//        futureChatResponse.join();

    }


}
